package com.wbxqy.car_management_system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wbxqy.car_management_system.dto.TimeSlot;
import com.wbxqy.car_management_system.entity.CarRequest;
import com.wbxqy.car_management_system.entity.User;
import com.wbxqy.car_management_system.service.ICarRequestService;
import com.wbxqy.car_management_system.service.ICarService;
import com.wbxqy.car_management_system.service.IDepartmentService;
import com.wbxqy.car_management_system.service.IUserService;
import com.wbxqy.car_management_system.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 司机 前端控制器
 *
 * @author wbxqy
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/wbxqy/driver")
public class DriverController {
    @Autowired
    private ICarRequestService carRequestService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 确定一个司机在一段时间是否有空
     *
     * @param driverId
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/is-free")
    public boolean isDriverFree(
            @RequestParam int driverId,
            @RequestParam String startTime,
            @RequestParam String endTime) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        return carRequestService.isDriverFree(driverId, start, end);
    }

    /**
     * 获取司机不空闲的时间段
     *
     * @param driverId
     * @return
     */
    @GetMapping("/busy-time-slots")
    public List<TimeSlot> getDriverBusyTimeSlots(@RequestParam int driverId) {
        return carRequestService.getDriverBusyTimeSlots(driverId);
    }

    /**
     * 获取在指定时间段内有空的所有司机
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/free-drivers")
    public List<User> getFreeDrivers(
            @RequestParam String startTime,
            @RequestParam String endTime) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime start = LocalDateTime.parse(startTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        return carRequestService.getFreeDrivers(start, end);
    }

    /**
     * 获取司机详情
     *
     * @param driverId
     * @return
     */
    @GetMapping("/detail")
    public User detail(@RequestParam Integer driverId) {
        User user = userService.getById(driverId);
        if (user.getDepartmentIdWbxqy() != null) {
            user.setDepartment(departmentService.getById(user.getDepartmentIdWbxqy()));
        }
        user.setUnfreeList(carRequestService.getDriverBusyTimeSlots(driverId));
        return user;

    }

    @GetMapping("/currentCarRequest")
    public CarRequest getCurrentCarRequest(@RequestHeader("Authorization") String token) {
        final User user = getUser(token);
        if (user!=null){
            final QueryWrapper<CarRequest> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("driver_id_wbxqy", user.getUserIdWbxqy());
            queryWrapper.eq("status_wbxqy", "A");
            final CarRequest one = carRequestService.getOne(queryWrapper);
            if (one!=null){
                return carRequestService.getFullRequest(one.getRequestIdWbxqy());
            }
        }
        return null;
    }

    private User getUser(String token) {
        final JSONObject tokenObj = (JSONObject) JSON.parse(JwtTokenUtil.getUsernameFromJWT(token));
        return userService.getById(tokenObj.get("userIdWbxqy").toString());
    }
}
